/**
 * UI管理
 */
class UIManager {
	private static _instance:UIManager = null;
	private _uiList:Dictionary<UIBase>;
	private imgLoad:eui.Image;
	// private ad;
	private isShow = false;

	public lampComp:LampCompUI;
	public constructor() {
		this._uiList = new Dictionary<UIBase>();
		this.imgLoad = new eui.Image();
		this.imgLoad.source = 'img_load_png';
	}

	public static instance(): UIManager {
		if (!UIManager._instance) {
            UIManager._instance = new UIManager();
        }
        return UIManager._instance;
	}

	/**
	 * 增加背景遮罩
	 */
	private getBalckBg(name):eui.Rect
    {
		let width = GlobalManager.instance().curWidth();
        let height = GlobalManager.instance().curHeight();
		let color = 0x000000;
        let rectBg = new eui.Rect(width, height, color);
        rectBg.alpha = 0.5;
        rectBg.visible = true;
        rectBg.name = "BlackMark_" + name;
        rectBg.horizontalCenter = rectBg.verticalCenter = 0;
        return rectBg;
    }

	/**
	 * 统一打开界面方法
	 * className: 界面类名
	 * data: 传入界面的数据
	 * 参数明细:
	 * {
	 * 		effect:number 打开弹窗类型 (默认弹窗是加的)
	 * 		showDark:boolean 是否加黑色遮罩 (默认弹窗是加的) 
	 * 		isPop:boolean  是否是弹窗界面
	 * 		isSole:boolean 是否关闭其他所有界面 (默认全屏界面是加的),
	 * 		showBG:boolean 是否允许点击空白区域关闭界面
	 * }
	 */
	public openUILogic(className, data?:Object)
	{
		let logic = null;
		let name:string = egret.getQualifiedClassName(className);
		
		if(GuideManager.instance().judgeIsInGuide(1, 1))
		{
			GuideManager.instance().hideGuide();
			GuideManager.instance().postGuideStepChange();
			GuideCache.instance().SaveGuideProcessByIdAndSubId(1,1);
		}

		if(!data)
		{
			data = {};
		}

		if(this._uiList.getValueByKey(name) == null)
        {
			logic = new className(data);
			//弹窗处理
            if(data["isPop"])
			{
				if(data["effect"] == null)
				data["effect"] = 1;
				GlobalManager.instance().addChildToLayer(this.getBalckBg(name), LayerType.UI);
			}
			else{
				data["isSole"] = true;
			}
			this._uiList.add(name, logic);
            LogUtil.log(`${name} logic is opening----`);

			// if(name == UIConst.GAME_UI || name == UIConst.NEW_WEAPONSELECT_UI)
			// {
			// 	this.hideBannerAd();
			// }
			// else
			// {
			// 	this.addBannerAd();
			// }
        }
        else
        {
            return null;
        }

		//添加界面处理
		if(logic && !logic.parent)
        {
			if(data['index'] != null)
				GlobalManager.instance().addChildToLayer(logic, LayerType.UI, data['index']);
			else
            	GlobalManager.instance().addChildToLayer(logic, LayerType.UI);
        }

		//唯一界面处理
        if(data["isSole"]){
			let list = this._uiList.keys;
			if(list.length > 1){
				for(let v of list){
					if(v != name){
                        this.closeUILogic(v);
					}
				}
			}
		}
		//特效处理
        if(data["effect"])
        {
			let effectType = data["effect"];
            EffectUtil.instance().OpenUIEffect(logic, effectType);
        }
	}

	protected removeScene(UIName:string,effectType:number = 0, list)
    {
        let logic = list.getValueByKey(UIName);	
		
        if(logic)
        {
            let parent = logic.parent;
            if(effectType != 0)
            {
                let call = function(){
					if(logic && logic.parent){//判断是否包含panel
						logic.parent.removeChild(logic);
						list.remove(UIName);
                }};
                EffectUtil.instance().CloseUIEffect(logic, effectType, call);
            }
            else
            {			
                if(logic && parent){//判断是否包含panel				
                    parent.removeChild(logic);
                    list.remove(UIName);
                }
            }
            let BalckBg:eui.Rect = parent.getChildByName("BlackMark_"+UIName);
            if(BalckBg)
            {
                BalckBg.parent.removeChild(BalckBg);
            }
        }
    }

	/**
	 * 统一关闭界面方法
	 * UIName: 界面名称
	 * 参数明细:
	 * {
	 * 		effect:number 打开弹窗类型 (默认弹窗是加的)
	 * }
	 */
	public closeUILogic(UIName:string, effectType?)
	{
		this.removeScene(UIName, effectType, this._uiList);
		// if(UIName == UIConst.GAME_UI || UIName == UIConst.NEW_WEAPONSELECT_UI)
		// {
		// 	this.addBannerAd();
		// }
		// let gameui = this.getLogicByName(UIConst.GAME_UI) as GameUI;
		// let weaopon = this.getLogicByName(UIConst.NEW_WEAPONSELECT_UI) as WeaponNewSelectUI;
		// if(gameui || weaopon)
		// {
		// 	this.hideBannerAd();
		// }
        LogUtil.log(`${UIName} logic is closeing----`); 
	}

	/**
	 * 展示广告
	 */
	public addBannerAd()
	{
		// if(this.isShow)return;
		// let random = Math.random();
		// let uri = random > 0.5 ? GameConst.adList.main_banner : GameConst.adList.other_banner;
		// if(!this.ad)
		// 	this.ad = WeChatgGameManager.instance().showBannerAd(uri);
		// if(this.ad)
		// this.ad.show();
		// this.isShow = true;
	}

	/**
	 * 隐藏广告
	 */
	public hideBannerAd(isClear:boolean = false)
	{
		// if(this.ad && this.isShow)
		// {
		// 	let self = this;
		// 	let afterHide = this.ad.hide();
		// 	if(afterHide)
		// 	{
		// 		afterHide.then(()=>{
		// 			if(isClear)
		// 			{
		// 				self.ad = null;
		// 			}
		// 			this.isShow = false;
		// 		});
		// 	}
			
		// }
	}

	public getLogicByName(UIName:string)
	{		
		if(this._uiList.getValueByKey(UIName))
        {
            return this._uiList.getValueByKey(UIName);
        }
        else
            return null;
	}

	//展示通用加载圈
	public showNormalLoading(isWhite?)
	{
		if(this.imgLoad.parent)
			return;
		let BalckBg = this.getBalckBg('Loading');
		if(isWhite)
			BalckBg.alpha = 0;
		GlobalManager.instance().addChildToLayer(BalckBg, LayerType.Tips);
		GlobalManager.instance().addChildToLayer(this.imgLoad, LayerType.Tips);
		this.imgLoad.anchorOffsetX = this.imgLoad.width >> 1;
		this.imgLoad.anchorOffsetY = this.imgLoad.height >> 1;
		this.imgLoad.x = this.imgLoad.parent.width >> 1;
		this.imgLoad.y = this.imgLoad.parent.height >> 1;
		EffectUtil.instance().rotationEffect(this.imgLoad);
	}

	public hideNormalLoading()
	{
		if(this.imgLoad.parent)
		{
			this.imgLoad.parent.removeChild(this.imgLoad);
			EffectUtil.instance().StopEffect(this.imgLoad);
		}
		
		let BalckBg = GlobalManager.instance().getLayer(LayerType.Tips).getChildByName("BlackMark_Loading");
		if(BalckBg)
		{
			BalckBg.parent.removeChild(BalckBg);
		}
	}

	/**
	 * 展示跑马灯
	 */
	public showLamp()
	{
		// if(!this.lampComp)
		// {
		// 	this.lampComp = new LampCompUI();
		// }
		// if(!this.lampComp.parent)
		// {
		// 	this.lampComp.anchorOffsetX = this.lampComp.width >> 1;

		// 	if(GlobalManager.instance().isIphonex())
		// 	{
		// 		this.lampComp.y = Math.floor(GlobalManager.instance().curHeight()/30) + 105;
		// 	}
		// 	else
		// 	{
		// 		this.lampComp.y = 105;
		// 	}
			
		// 	GlobalManager.instance().addChildToLayer(this.lampComp, LayerType.Tips);
		// 	this.lampComp.x = this.lampComp.parent.width >> 1;
		// }
		// this.lampComp.visible = true;
	}

	/**
	 * 隐藏跑马灯
	 */
	public hideLamp()
	{
		// if(!this.lampComp)return;
		// this.lampComp.visible = false;
	}
}